var fs = require('fs')
  , path = require('path')
  , opentype = require('opentype.js')
  , _ = require('underscore')
var fontsPath = path.join(process.cwd(),'node_modules/fed-lib/assets/iconfont/iconfont.ttf')
  , tmpl = _.template(fs.readFileSync(path.join(__dirname,'iconfonts.html')).toString())
module.exports = function(server){
  server.get('/iconfonts',function(req,res,next){
    opentype.load(fontsPath, function(err, info) {
      if(err){
        console.log(err)
        next()
      }else{
        res.end(tmpl({
          iconfonts:Object.keys(info.glyphs.glyphs).map(function(k){
            var g = info.glyphs.glyphs[k]
            return {
              code:g.unicode?'&#x'+g.unicode.toString(16)+';':null
            }
          }).filter(function(i){
            return !!i.code
          }),
          req:req
        }))
      }
    });
  })
}
